<template>
  <div class="pulse-selection-modal">
    <div class="modal-header">
      <span style="color: black">脉象选择</span>
      <button class="close-button" @click="closeModal">×</button>
    </div>
    <div class="modal-body">
      <!-- 左右选择 -->
      <div class="pulse-tree" style="width: 350px; margin: 0 auto">
        <div class="pulse-category">
          <div
            class="pulse-node"
            :class="{ 'pulse-node-active': isPositionSelected('左') }"
            @click="togglePosition('左')"
          >
            左
          </div>
        </div>
        <div class="pulse-category">
          <div
            class="pulse-node"
            :class="{ 'pulse-node-active': isPositionSelected('右') }"
            @click="togglePosition('右')"
          >
            右
          </div>
        </div>
      </div>

      <!-- 寸关尺选择 -->
      <div class="pulse-tree">
        <div class="pulse-category">
          <div
            class="pulse-node"
            :class="{ 'pulse-node-active': isLocationSelected('寸') }"
            @click="toggleLocation('寸')"
          >
            寸
          </div>
        </div>
        <div class="pulse-category">
          <div
            class="pulse-node"
            :class="{ 'pulse-node-active': isLocationSelected('关') }"
            @click="toggleLocation('关')"
          >
            关
          </div>
        </div>
        <div class="pulse-category">
          <div
            class="pulse-node"
            :class="{ 'pulse-node-active': isLocationSelected('尺') }"
            @click="toggleLocation('尺')"
          >
            尺
          </div>
        </div>
      </div>

      <!-- 脉象选择 -->
      <div
        v-for="(venation_group, groupIndex) in venationList"
        :key="groupIndex"
        class="pulse-tree"
      >
        <div
          v-for="venation_item in venation_group"
          :key="venation_item.id"
          class="pulse-category"
        >
          <div
            class="pulse-node"
            :class="{
              'pulse-node-active': isVenationSelected(venation_item.name),
            }"
            @click="toggleVenation(venation_item)"
          >
            {{ venation_item.name }}
          </div>
        </div>
      </div>
    </div>
    <div class="modal-footer">
      <span class="selected-text">
        <span v-for="value in selectedItems">{{ value }}</span>
      </span>
      <button class="confirm-button" @click="onConfirm">确定</button>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
const emits = defineEmits(['closeModal', 'onConfirm']);
// 脉象数据
const venationList = ref([
  [
    { id: 1, name: '浮' },
    { id: 2, name: '沉' },
    { id: 3, name: '迟' },
    { id: 4, name: '数' },
    { id: 5, name: '虚' },
    { id: 6, name: '实' },
  ],
  [
    { id: 7, name: '懦' },
    { id: 8, name: '弱' },
    { id: 9, name: '缓' },
    { id: 12, name: '促' },
    { id: 10, name: '细 ' },
    { id: 11, name: '弦' },
  ],
  [
    { id: 13, name: '洪' },
    { id: 14, name: '伏' },
    { id: 15, name: '涩' },
    { id: 16, name: '动' },
    { id: 17, name: '微 ' },
    { id: 18, name: '滑' },
  ],
  [
    { id: 19, name: '芤' },
    { id: 20, name: '牢' },
    { id: 21, name: '结' },
    { id: 22, name: '疾' },
    { id: 23, name: '代 ' },
    { id: 24, name: '紧' },
  ],
]);

// 选中状态管理
const selectedItems = ref<Set<string>>(new Set());
const selectedPosition = ref<string>(''); // 左/右
const selectedLocation = ref<string>(''); // 寸/关/尺

// 切换位置选择（左/右）
const togglePosition = (position: string) => {
  selectedPosition.value = selectedPosition.value === position ? '' : position;
  if (selectedItems.value.has(position)) {
    selectedItems.value.delete(position);
  } else {
    selectedItems.value.add(position);
  }
};

// 切换部位选择（寸/关/尺）
const toggleLocation = (location: string) => {
  selectedLocation.value = selectedLocation.value === location ? '' : location;
  if (selectedItems.value.has(location)) {
    selectedItems.value.delete(location);
  } else {
    selectedItems.value.add(location);
  }
};

// 切换脉象选择
const toggleVenation = (item: { id: number; name: string }) => {
  if (selectedItems.value.has(item.name)) {
    selectedItems.value.delete(item.name);
  } else {
    selectedItems.value.add(item.name);
  }
};

// 检查是否选中
const isPositionSelected = (position: string) => {
  return selectedPosition.value === position;
};

const isLocationSelected = (location: string) => {
  return selectedLocation.value === location;
};

const isVenationSelected = (name: string) => {
  return selectedItems.value.has(name);
};

// 获取选中文本
const getSelectedText = () => {
  let result = '';
  if (selectedPosition.value) result += selectedPosition.value;
  if (selectedLocation.value) result += selectedLocation.value;

  venationList.value.flat().forEach((item) => {
    if (selectedItems.value.has(item.name)) {
      result += item.name.trim();
    }
  });

  return result || '请选择脉象';
};

const closeModal = () => {
  // Logic to close the modal
  emits('closeModal');
  //console.log('Modal closed');
};
const onConfirm = () => {
  emits('onConfirm', selectedItems.value);
  closeModal();
};
</script>

<style lang="scss" scoped>
.pulse-selection-modal {
  width: 475px;
  height: 400px;
  background: #ffffff;
  box-shadow: 0px 8px 32px 0px rgba(0, 0, 0, 0.15),
    0px 4px 12px 0px rgba(0, 0, 0, 0.08);
  border-radius: 12px 12px 12px 12px;
  overflow: hidden;
  .modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;

    span {
      font-weight: bold;
    }

    .close-button {
      background: none;
      border: none;
      cursor: pointer;
      font-size: 20px;
    }
  }

  .modal-body {
    .pulse-tree {
      display: flex;
      justify-content: space-around;
      width: 100%;
      .pulse-category {
        display: flex;
        flex-direction: column;
        align-items: center;
        .pulse-node {
          border-radius: 50%;
          color: rgb(0, 0, 0);
          display: flex;
          justify-content: center;
          align-items: center;
          margin-bottom: 10px;
          cursor: pointer;
          width: 40px;
          height: 40px;
          border-radius: 50px 50px 50px 50px;
          border: 1px solid #e7e7e7;
        }
        .pulse-node-active {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          background-color: #00bfa5;
          color: white;
          display: flex;
          justify-content: center;
          align-items: center;
          margin-bottom: 10px;
          cursor: pointer;
        }

        .pulse-subcategory {
          display: flex;
          flex-direction: column;
          align-items: center;

          .pulse-sub-subcategory {
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
            margin-top: 5px;

            .pulse-item {
              width: 30px;
              height: 30px;
              border-radius: 50%;
              background-color: #e0f7fa;
              color: #00796b;
              display: flex;
              justify-content: center;
              align-items: center;
              margin: 5px;
              cursor: pointer;
            }
          }
        }
      }
    }
  }

  .modal-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    background: rgba(22, 173, 164, 0.08);
    margin-top: 10px;
    .selected-text {
      font-size: 14px;
    }

    .confirm-button {
      background-color: #00bfa5;
      color: white;
      border: none;
      border-radius: 3px;
      padding: 5px 10px;
      cursor: pointer;
    }
  }
}
</style>
